package cn.shyouchi.wuguanjia.android.ui.home.security.thread.message.matter;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.chad.library.adapter.base.entity.MultiItemEntity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.api.matter.MatterApi;
import cn.shyouchi.wuguanjia.android.base.BaseActivity;
import cn.shyouchi.wuguanjia.android.common.Constant;
import cn.shyouchi.wuguanjia.android.model.BaseRequestResult;
import cn.shyouchi.wuguanjia.android.subscribers.ProgressSubscriber;
import cn.shyouchi.wuguanjia.android.ui.home.security.thread.message.birthday.BirthdayNotice;
import cn.shyouchi.wuguanjia.android.ui.home.security.thread.message.casual.CasualNotice;
import cn.shyouchi.wuguanjia.android.widget.TopBar;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class MatterNoticeActivity extends BaseActivity {

    @BindView(R.id.topBar)
    TopBar mTopBar;
    @BindView(R.id.rv_msg_list)
    RecyclerView mRvMsgList;

    List<MultiItemEntity> mItemEntities = new ArrayList<>();
    MatterNoticeAdapter mMatterNoticeAdapter;

    MatterApi mMatterApi;

    int src;

    @Override
    public int initContentView() {
        return R.layout.activity_matter_notice;
    }

    @Override
    public void initInjector() {
        mMatterApi = new MatterApi();
        src = getIntent().getIntExtra("src", 0);
    }

    @Override
    public void initUiAndListener() {
        ButterKnife.bind(this);
        mTopBar.setLeftIcon(R.mipmap.ic_back);
        mTopBar.setTitle(getTitle().toString());
        setSupportActionBar(mTopBar.getToolbar());

        mMatterNoticeAdapter = new MatterNoticeAdapter(mItemEntities);
        mRvMsgList.setAdapter(mMatterNoticeAdapter);
        mRvMsgList.setLayoutManager(new LinearLayoutManager(mContext));


        if (src == 0) {
            fetchMatter();
            mTopBar.setTitle("待办事项提醒");
        } else if (src == Constant.MATTER_TYPE_BIRTHDAY) {
            fetchBirthday();
            mTopBar.setTitle("生日提醒");
        } else if (src == Constant.MATTER_TYPE_MEET_ERROR) {
            fetchError();
            mTopBar.setTitle("随手拍");
        }


    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isApplyStatusBarColor() {
        return true;
    }


    private void fetchMatter() {
        mMatterApi.getMatters()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber<BaseRequestResult<List<MatterNotice>>>(mContext) {
                    @Override
                    public void onNext(BaseRequestResult<List<MatterNotice>> result) {
                        mItemEntities.clear();
                        mItemEntities.addAll(result.getResultMessage());
                        mMatterNoticeAdapter.notifyDataSetChanged();
                    }
                });
    }

    private void fetchBirthday() {
        mMatterApi.getBirthdayList()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber<BaseRequestResult<List<BirthdayNotice>>>(mContext) {
                    @Override
                    public void onNext(BaseRequestResult<List<BirthdayNotice>> result) {
                        mItemEntities.clear();
                        mItemEntities.addAll(result.getResultMessage());
                        mMatterNoticeAdapter.notifyDataSetChanged();
                    }
                });
    }

    private void fetchError() {


        new MatterApi().getErrorDetail()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber<BaseRequestResult<List<CasualNotice>>>(mContext) {
                    @Override
                    public void onNext(BaseRequestResult<List<CasualNotice>> result) {
                        mItemEntities.clear();
                        mItemEntities.addAll(result.getResultMessage());
                        mMatterNoticeAdapter.notifyDataSetChanged();
                    }
                });


//
//        mMatterApi.getErrorList()
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new ProgressSubscriber<BaseRequestResult<List<CasualNotice>>>(mContext) {
//                    @Override
//                    public void onNext(BaseRequestResult<List<CasualNotice>> result) {
//
//                    }
//                });
    }


}
